﻿/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using ASN1Decoder.Builders.Nodes;
using System.Drawing;
using Exceptions;
using Core.Model.Data.Generics;
using Core.Model.Groups;

namespace ASN1Decoder.Builders
{
    internal class ColourBuilder
    {
        internal static GenericColour Build(ASN1Node node, Group group)
        {
            GenericColour colour = null;

            if (node is OctetStringNode)
            {
                colour = new GenericColour(LookupColourFromString(node.StringValue));


            }
            else if (node is TaggedNode)
            {

                TaggedNode n = (TaggedNode) node;
                TaggedNode e;

                if (n.Tag == ASN1ContextCodes.C_NEW_COLOUR_INDEX)
                    throw new OpenMhegException("No CLUT defined in 1.06");

                else if (n.Tag == ASN1ContextCodes.C_NEW_ABSOLUTE_COLOUR)
                {
                    
                    if ((e = n[ASN1ContextCodes.C_INDIRECTREFERENCE]) != null)
                        colour = new GenericColour(ObjectRefBuilder.Build(e.First, group));
                    else
                        colour = LookupGenericColour(n.First.StringValue);
                }
                else
                    throw new OpenMhegException("Unknown Colour");

            }
            else throw new Exception("Fix me");

            return colour;

        }

        private static GenericColour Lookup(int index)
        {
            throw new OpenMhegException("No CLUT defined in 1.06");
            //return Color.FromArgb( 255, 0, 255 );
        }


        internal static GenericColour LookupGenericColour(String name)
        {
            return new GenericColour(LookupColourFromString(name));
        }

        internal static Color LookupColourFromString(String colour)
        {
            return LookupColour((new ASCIIEncoding()).GetBytes(colour));
        }

        private static Color LookupColour(byte[] components)
        {

            if (components.Length > 4)
                throw new Exception("oi");
            if (components.Length < 4)
                //           return Color.HotPink;
                throw new Exception("oi");
            // Color colour;

            int r = 0;
            int g = 0;
            int b = 0;
            int a = 255;

            //if ( name.Equals( "white", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.White;
            //else if ( name.Equals( "gray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Gray;
            //else if ( name.Equals( "darkgray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkGray;
            //else if ( name.Equals( "lightgray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.LightGray;
            //else if ( name.Equals( "black", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Black;
            //else if ( name.Equals( "red", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Red;
            //else if ( name.Equals( "darkred", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkRed;
            //else if ( name.Equals( "blue", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Blue;
            //else if ( name.Equals( "darkblue", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkBlue;
            //else if ( name.Equals( "green", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Green;
            //else if ( name.Equals( "darkgreen", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkGreen;
            //else if ( name.Equals( "yellow", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Yellow;
            //else if ( name.Equals( "darkyellow", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Yellow;
            //else if ( name.Equals( "cyan", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Cyan;
            //else if ( name.Equals( "darkcyan", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkCyan;
            //else if ( name.Equals( "magenta", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Magenta;
            //else if ( name.Equals( "darkmagenta", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkMagenta;
            //else if ( name.Equals( "orange", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Orange;
            //else if ( name.Equals( "pink", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Pink;
            //else if ( name.Equals( "transparent", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.FromArgb( 0, 0, 0, 0 );
            //else if ( name.StartsWith( "#" ) && name.Length == 7 )
            //{
            //    throw new Exception( "Code me" );
            //    //r = DecodeUtils.ParseHex( name.substring( 1, 3 ), 16 );
            //    //g = DecodeUtils.ParseHex( name.substring( 3, 5 ), 16 );
            //    //b = DecodeUtils.ParseHex( name.substring( 5, 7 ), 16 );
            //    //colour = new Color( r, g, b );
            //}
            //else if ( name.StartsWith( "=" ) && name.Length == 10 )
            //{
            //    throw new Exception( "Code me" );
            //    //r = DecodeUtils.ParseHex( name.Substring( 2, 4 ));
            //    //g = DecodeUtils.ParseHex( name.substring( 5, 7 ));
            //    //b = DecodeUtils.ParseHex( name.substring( 8, 10 ));
            //    //a = 255 - Builder.parseInt( name.substring( 11, 13 ), 16 );
            //    //colour = new Color( r, g, b, a );
            //}
            //else if ( name.Length == 3 )
            //{
            //    r = ( int ) name [0];
            //    g = ( int ) name [1];
            //    b = ( int ) name [2];
            //    colour = Color.FromArgb( r, g, b );
            //}
            //else if ( name.Length == 4 )
            //{
            //    r = ( int ) name [0];
            //    g = ( int ) name [1];
            //    b = ( int ) name [2];
            //    a = 255 - ( int ) name [3];
            //    //a = ( int ) name [3];
            //    colour = Color.FromArgb( a, r, g, b );
            //}
            //  else
            //  {

            if (components.Length > 0)
            {
                r = components[0];// ( int ) components [0] + ( int ) components [1];

                if (components.Length > 1)
                {
                    g = components[1];//( int ) components [2] + ( int ) components [3];
                }

                if (components.Length > 2)
                {
                    b = components[2];//( int ) components [4] + ( int ) components [5];
                }

                if (components.Length > 3)
                {
                    a = 255 - components[3];//(( int ) name [3]);
                    //a = ( int ) name [3];
                }

            }
            return Color.FromArgb(a, r, g, b);
            //  }
            // else
            // {
            //throw new Exception( "oi" );
            //log.warning( "Couldn't parse a colour from string " + name );
            //colour = Color.White;
            //    colour = Color.FromArgb( a, r, g, b );
            //}

            //return colour;
        }
    }
}

